// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

#include "DBManager/Repository/HDF5/Input/Design/DRESP3Repository.h"

#include "DBManager/Repository/HDF5/Base/HDF5RepositoryMacro.h"

HDF5_REG_REPOSITORY (DRESP3Repository)

using namespace SG::DBManager::HDF5;
using namespace SG::DataStructure;
using namespace SG::DataStructure::FEM;
using namespace SG::DataStructure::Common;

// 构造函数实现
DRESP3Repository::DRESP3Repository (HDF5Database& database) : SG::DBManager::HDF5::MainlyRepositoryBase<MainStruct> (database)
{}

void DRESP3Repository::InitialDatasetInfo ()
{
    RegisterTableInfo<SG::DataStructure::Common::Id> ("/OWN/INDEX/INPUT/DESIGN/DRESP3", IndexDataset);
    RegisterTableInfo<NasMainStruct> ("/SG/INPUT/DESIGN/DRESP3/IDENTITY", MainDataset);
    RegisterTableInfo<SubStructDESVAR> ("/SG/INPUT/DESIGN/DRESP3/DESVAR", SubDataset_DESVAR);
    RegisterTableInfo<SubStructDNODE> ("/SG/INPUT/DESIGN/DRESP3/DNODE", SubDataset_DNODE);
    RegisterTableInfo<SubStructDRESP1> ("/SG/INPUT/DESIGN/DRESP3/DRESP1", SubDataset_DRESP1);
    RegisterTableInfo<SubStructDRESP2> ("/SG/INPUT/DESIGN/DRESP3/DRESP2", SubDataset_DRESP2);
    RegisterTableInfo<SubStructDTABLE> ("/SG/INPUT/DESIGN/DRESP3/DTABLE", SubDataset_DTABLE);
    RegisterTableInfo<SubStructDVCREL1> ("/SG/INPUT/DESIGN/DRESP3/DVCREL1", SubDataset_DVCREL1);
    RegisterTableInfo<SubStructDVCREL2> ("/SG/INPUT/DESIGN/DRESP3/DVCREL2", SubDataset_DVCREL2);
    RegisterTableInfo<SubStructDVLREL1> ("/SG/INPUT/DESIGN/DRESP3/DVLREL1", SubDataset_DVLREL1);
    RegisterTableInfo<SubStructDVMREL1> ("/SG/INPUT/DESIGN/DRESP3/DVMREL1", SubDataset_DVMREL1);
    RegisterTableInfo<SubStructDVMREL2> ("/SG/INPUT/DESIGN/DRESP3/DVMREL2", SubDataset_DVMREL2);
    RegisterTableInfo<SubStructDVPREL1> ("/SG/INPUT/DESIGN/DRESP3/DVPREL1", SubDataset_DVPREL1);
    RegisterTableInfo<SubStructDVPREL2> ("/SG/INPUT/DESIGN/DRESP3/DVPREL2", SubDataset_DVPREL2);
    RegisterTableInfo<SubStructGRDTYP> ("/SG/INPUT/DESIGN/DRESP3/GRDTYP", SubDataset_GRDTYP);
    RegisterTableInfo<SubStructUSRDATA> ("/SG/INPUT/DESIGN/DRESP3/USRDATA", SubDataset_USRDATA);
}

void DRESP3Repository::DumpWriteCahce ()
{
    auto& writeStorage = this->GetMainCache ().m_writeStorage;

    // 1. flag设置为无顺序
    GetMainDatasetInfo ().SetIsSequential (false);

    // 主表数据
    std::vector<NasMainStruct> IDENTITYData{};
    // 附表数据
    std::vector<SubStructDESVAR>  subDataDESVAR;
    std::vector<SubStructDNODE>   subDataDNODE;
    std::vector<SubStructDRESP1>  subDataDRESP1;
    std::vector<SubStructDRESP2>  subDataDRESP2;
    std::vector<SubStructDTABLE>  subDataDTABLE;
    std::vector<SubStructDVCREL1> subDataDVCREL1;
    std::vector<SubStructDVCREL2> subDataDVCREL2;
    std::vector<SubStructDVLREL1> subDataDVLREL1;
    std::vector<SubStructDVMREL1> subDataDVMREL1;
    std::vector<SubStructDVMREL2> subDataDVMREL2;
    std::vector<SubStructDVPREL1> subDataDVPREL1;
    std::vector<SubStructDVPREL2> subDataDVPREL2;
    std::vector<SubStructGRDTYP>  subDataGRDTYP;
    std::vector<SubStructUSRDATA> subDataUSRDATA;

    // 2. 转换为nastran格式数据
    OwnToNastran (writeStorage,
                  IDENTITYData,
                  subDataDESVAR,
                  subDataDNODE,
                  subDataDRESP1,
                  subDataDRESP2,
                  subDataDTABLE,
                  subDataDVCREL1,
                  subDataDVCREL2,
                  subDataDVLREL1,
                  subDataDVMREL1,
                  subDataDVMREL2,
                  subDataDVPREL1,
                  subDataDVPREL2,
                  subDataGRDTYP,
                  subDataUSRDATA);

    // 3. 写出数据至文件
    WriteData (MainDataset, IDENTITYData);
    WriteData (SubDataset_DESVAR, subDataDESVAR);
    WriteData (SubDataset_DNODE, subDataDNODE);
    WriteData (SubDataset_DRESP1, subDataDRESP1);
    WriteData (SubDataset_DRESP2, subDataDRESP2);
    WriteData (SubDataset_DTABLE, subDataDTABLE);
    WriteData (SubDataset_DVCREL1, subDataDVCREL1);
    WriteData (SubDataset_DVCREL2, subDataDVCREL2);
    WriteData (SubDataset_DVLREL1, subDataDVLREL1);
    WriteData (SubDataset_DVMREL1, subDataDVMREL1);
    WriteData (SubDataset_DVMREL2, subDataDVMREL2);
    WriteData (SubDataset_DVPREL1, subDataDVPREL1);
    WriteData (SubDataset_DVPREL2, subDataDVPREL2);
    WriteData (SubDataset_GRDTYP, subDataGRDTYP);
    WriteData (SubDataset_USRDATA, subDataUSRDATA);

    // 4. 更新Index表
    this->WriteIndexData (this->GetIndexDatasetInfo (), writeStorage);

    // 5. 清空缓存
    writeStorage.clear ();
}

std::vector<DRESP3Repository::DataType>*  DRESP3Repository::LoadReadCache (std::size_t loadIndex)
{
    // 1. 读取范围内主表数据
    std::vector<NasMainStruct> IDENTITYData;
    LoadMainMetadata (loadIndex, IDENTITYData);

    if (IDENTITYData.empty ())
    {
        return nullptr;
    }

    // 2. 读取范围内附表数据
    std::vector<SubStructDESVAR>  subDataDESVAR;
    std::vector<SubStructDNODE>   subDataDNODE;
    std::vector<SubStructDRESP1>  subDataDRESP1;
    std::vector<SubStructDRESP2>  subDataDRESP2;
    std::vector<SubStructDTABLE>  subDataDTABLE;
    std::vector<SubStructDVCREL1> subDataDVCREL1;
    std::vector<SubStructDVCREL2> subDataDVCREL2;
    std::vector<SubStructDVLREL1> subDataDVLREL1;
    std::vector<SubStructDVMREL1> subDataDVMREL1;
    std::vector<SubStructDVMREL2> subDataDVMREL2;
    std::vector<SubStructDVPREL1> subDataDVPREL1;
    std::vector<SubStructDVPREL2> subDataDVPREL2;
    std::vector<SubStructGRDTYP>  subDataGRDTYP;
    std::vector<SubStructUSRDATA> subDataUSRDATA;

    LoadSecondaryMetadata (IDENTITYData, DESVAR_POS, DESVAR_LEN, SubDataset_DESVAR, subDataDESVAR);
    LoadSecondaryMetadata (IDENTITYData, DNODE_POS, DNODE_LEN, SubDataset_DNODE, subDataDNODE);
    LoadSecondaryMetadata (IDENTITYData, DRESP1_POS, DRESP1_LEN, SubDataset_DRESP1, subDataDRESP1);
    LoadSecondaryMetadata (IDENTITYData, DRESP2_POS, DRESP2_LEN, SubDataset_DRESP2, subDataDRESP2);
    LoadSecondaryMetadata (IDENTITYData, DTABLE_POS, DTABLE_LEN, SubDataset_DTABLE, subDataDTABLE);
    LoadSecondaryMetadata (IDENTITYData, DVCREL1_POS, DVCREL1_LEN, SubDataset_DVCREL1, subDataDVCREL1);
    LoadSecondaryMetadata (IDENTITYData, DVCREL2_POS, DVCREL2_LEN, SubDataset_DVCREL2, subDataDVCREL2);
    LoadSecondaryMetadata (IDENTITYData, DVLREL1_POS, DVLREL1_LEN, SubDataset_DVLREL1, subDataDVLREL1);
    LoadSecondaryMetadata (IDENTITYData, DVMREL1_POS, DVMREL1_LEN, SubDataset_DVMREL1, subDataDVMREL1);
    LoadSecondaryMetadata (IDENTITYData, DVMREL2_POS, DVMREL2_LEN, SubDataset_DVMREL2, subDataDVMREL2);
    LoadSecondaryMetadata (IDENTITYData, DVPREL1_POS, DVPREL1_LEN, SubDataset_DVPREL1, subDataDVPREL1);
    LoadSecondaryMetadata (IDENTITYData, DVPREL2_POS, DVPREL2_LEN, SubDataset_DVPREL2, subDataDVPREL2);
    LoadSecondaryMetadata (IDENTITYData, GRDTYP_POS, GRDTYP_LEN, SubDataset_GRDTYP, subDataGRDTYP);
    LoadSecondaryMetadata (IDENTITYData, USRDATA_POS, USRDATA_LEN, SubDataset_USRDATA, subDataUSRDATA);

    // 3. 将数据转换为自己的格式放入缓存
    auto* cacheVector = m_cacheHelper.LookupOrCreateVector<DataType> (GetMainCache ().m_readCacheKey, IDENTITYData.size ());
    NastranToOwn (IDENTITYData,
                  subDataDESVAR,
                  subDataDNODE,
                  subDataDRESP1,
                  subDataDRESP2,
                  subDataDTABLE,
                  subDataDVCREL1,
                  subDataDVCREL2,
                  subDataDVLREL1,
                  subDataDVMREL1,
                  subDataDVMREL2,
                  subDataDVPREL1,
                  subDataDVPREL2,
                  subDataGRDTYP,
                  subDataUSRDATA,
                  *cacheVector);
    return cacheVector;
}

void DRESP3Repository::SortData ()
{
    // 1. 读取主表数据
    std::vector<NasMainStruct> IDENTITYData;
    GetDataset (MainDataset).read (IDENTITYData);

    if (IDENTITYData.empty ())
    {
        GetMainDatasetInfo ().SetIsSequential (true);
        return;
    }

    // 2. 读取附表数据
    std::vector<SubStructDESVAR>  subDataDESVAR;
    std::vector<SubStructDNODE>   subDataDNODE;
    std::vector<SubStructDRESP1>  subDataDRESP1;
    std::vector<SubStructDRESP2>  subDataDRESP2;
    std::vector<SubStructDTABLE>  subDataDTABLE;
    std::vector<SubStructDVCREL1> subDataDVCREL1;
    std::vector<SubStructDVCREL2> subDataDVCREL2;
    std::vector<SubStructDVLREL1> subDataDVLREL1;
    std::vector<SubStructDVMREL1> subDataDVMREL1;
    std::vector<SubStructDVMREL2> subDataDVMREL2;
    std::vector<SubStructDVPREL1> subDataDVPREL1;
    std::vector<SubStructDVPREL2> subDataDVPREL2;
    std::vector<SubStructGRDTYP>  subDataGRDTYP;
    std::vector<SubStructUSRDATA> subDataUSRDATA;

    GetDataset (SubDataset_DESVAR).read (subDataDESVAR);
    GetDataset (SubDataset_DNODE).read (subDataDNODE);
    GetDataset (SubDataset_DRESP1).read (subDataDRESP1);
    GetDataset (SubDataset_DRESP2).read (subDataDRESP2);
    GetDataset (SubDataset_DTABLE).read (subDataDTABLE);
    GetDataset (SubDataset_DVCREL1).read (subDataDVCREL1);
    GetDataset (SubDataset_DVCREL2).read (subDataDVCREL2);
    GetDataset (SubDataset_DVLREL1).read (subDataDVLREL1);
    GetDataset (SubDataset_DVMREL1).read (subDataDVMREL1);
    GetDataset (SubDataset_DVMREL2).read (subDataDVMREL2);
    GetDataset (SubDataset_DVPREL1).read (subDataDVPREL1);
    GetDataset (SubDataset_DVPREL2).read (subDataDVPREL2);
    GetDataset (SubDataset_GRDTYP).read (subDataGRDTYP);
    GetDataset (SubDataset_USRDATA).read (subDataUSRDATA);

    // 3. 将所有数据转换
    std::vector<MainStruct> AllData{};
    NastranToOwn (IDENTITYData,
                  subDataDESVAR,
                  subDataDNODE,
                  subDataDRESP1,
                  subDataDRESP2,
                  subDataDTABLE,
                  subDataDVCREL1,
                  subDataDVCREL2,
                  subDataDVLREL1,
                  subDataDVMREL1,
                  subDataDVMREL2,
                  subDataDVPREL1,
                  subDataDVPREL2,
                  subDataGRDTYP,
                  subDataUSRDATA,
                  AllData);

    // 4. 排序
    std::sort (AllData.begin (), AllData.end ());

    // 5. 转换回nastran格式
    OwnToNastran (AllData,
                  IDENTITYData,
                  subDataDESVAR,
                  subDataDNODE,
                  subDataDRESP1,
                  subDataDRESP2,
                  subDataDTABLE,
                  subDataDVCREL1,
                  subDataDVCREL2,
                  subDataDVLREL1,
                  subDataDVMREL1,
                  subDataDVMREL2,
                  subDataDVPREL1,
                  subDataDVPREL2,
                  subDataGRDTYP,
                  subDataUSRDATA,
                  true);

    // 6. 写出主表数据至文件
    GetDataset (MainDataset).write (IDENTITYData);
    // 7. 写出附表数据至文件
    GetDataset (SubDataset_DESVAR).write (subDataDESVAR);
    GetDataset (SubDataset_DNODE).write (subDataDNODE);
    GetDataset (SubDataset_DRESP1).write (subDataDRESP1);
    GetDataset (SubDataset_DRESP2).write (subDataDRESP2);
    GetDataset (SubDataset_DTABLE).write (subDataDTABLE);
    GetDataset (SubDataset_DVCREL1).write (subDataDVCREL1);
    GetDataset (SubDataset_DVCREL2).write (subDataDVCREL2);
    GetDataset (SubDataset_DVLREL1).write (subDataDVLREL1);
    GetDataset (SubDataset_DVMREL1).write (subDataDVMREL1);
    GetDataset (SubDataset_DVMREL2).write (subDataDVMREL2);
    GetDataset (SubDataset_DVPREL1).write (subDataDVPREL1);
    GetDataset (SubDataset_DVPREL2).write (subDataDVPREL2);
    GetDataset (SubDataset_GRDTYP).write (subDataGRDTYP);
    GetDataset (SubDataset_USRDATA).write (subDataUSRDATA);

    // 8. 重写索引
    RewriteIndexData (AllData);

    // 9. 设置为已顺序
    GetMainDatasetInfo ().SetIsSequential (true);
}

std::vector<DRESP3Repository::MainStruct> DRESP3Repository::GetAllData ()
{
    Sort ();

    std::vector<MainStruct> result;

    std::vector<NasMainStruct> IDENTITYData;
    GetDataset (MainDataset).read (IDENTITYData);

    if (IDENTITYData.empty ())
    {
        return result;
    }

    std::vector<SubStructDESVAR>  subDataDESVAR;
    std::vector<SubStructDNODE>   subDataDNODE;
    std::vector<SubStructDRESP1>  subDataDRESP1;
    std::vector<SubStructDRESP2>  subDataDRESP2;
    std::vector<SubStructDTABLE>  subDataDTABLE;
    std::vector<SubStructDVCREL1> subDataDVCREL1;
    std::vector<SubStructDVCREL2> subDataDVCREL2;
    std::vector<SubStructDVLREL1> subDataDVLREL1;
    std::vector<SubStructDVMREL1> subDataDVMREL1;
    std::vector<SubStructDVMREL2> subDataDVMREL2;
    std::vector<SubStructDVPREL1> subDataDVPREL1;
    std::vector<SubStructDVPREL2> subDataDVPREL2;
    std::vector<SubStructGRDTYP>  subDataGRDTYP;
    std::vector<SubStructUSRDATA> subDataUSRDATA;

    GetDataset (SubDataset_DESVAR).read (subDataDESVAR);
    GetDataset (SubDataset_DNODE).read (subDataDNODE);
    GetDataset (SubDataset_DRESP1).read (subDataDRESP1);
    GetDataset (SubDataset_DRESP2).read (subDataDRESP2);
    GetDataset (SubDataset_DTABLE).read (subDataDTABLE);
    GetDataset (SubDataset_DVCREL1).read (subDataDVCREL1);
    GetDataset (SubDataset_DVCREL2).read (subDataDVCREL2);
    GetDataset (SubDataset_DVLREL1).read (subDataDVLREL1);
    GetDataset (SubDataset_DVMREL1).read (subDataDVMREL1);
    GetDataset (SubDataset_DVMREL2).read (subDataDVMREL2);
    GetDataset (SubDataset_DVPREL1).read (subDataDVPREL1);
    GetDataset (SubDataset_DVPREL2).read (subDataDVPREL2);
    GetDataset (SubDataset_GRDTYP).read (subDataGRDTYP);
    GetDataset (SubDataset_USRDATA).read (subDataUSRDATA);

    // 将所有数据转换
    NastranToOwn (IDENTITYData,
                  subDataDESVAR,
                  subDataDNODE,
                  subDataDRESP1,
                  subDataDRESP2,
                  subDataDTABLE,
                  subDataDVCREL1,
                  subDataDVCREL2,
                  subDataDVLREL1,
                  subDataDVMREL1,
                  subDataDVMREL2,
                  subDataDVPREL1,
                  subDataDVPREL2,
                  subDataGRDTYP,
                  subDataUSRDATA,
                  result);

    return result;
}

void DRESP3Repository::OwnToNastran (std::vector<MainStruct>&            own,
                                     std::vector<NasMainStruct>& _OUT    IDENTITYData,
                                     std::vector<SubStructDESVAR>& _OUT  subDataDESVAR,
                                     std::vector<SubStructDNODE>& _OUT   subDataDNODE,
                                     std::vector<SubStructDRESP1>& _OUT  subDataDRESP1,
                                     std::vector<SubStructDRESP2>& _OUT  subDataDRESP2,
                                     std::vector<SubStructDTABLE>& _OUT  subDataDTABLE,
                                     std::vector<SubStructDVCREL1>& _OUT subDataDVCREL1,
                                     std::vector<SubStructDVCREL2>& _OUT subDataDVCREL2,
                                     std::vector<SubStructDVLREL1>& _OUT subDataDVLREL1,
                                     std::vector<SubStructDVMREL1>& _OUT subDataDVMREL1,
                                     std::vector<SubStructDVMREL2>& _OUT subDataDVMREL2,
                                     std::vector<SubStructDVPREL1>& _OUT subDataDVPREL1,
                                     std::vector<SubStructDVPREL2>& _OUT subDataDVPREL2,
                                     std::vector<SubStructGRDTYP>& _OUT  subDataGRDTYP,
                                     std::vector<SubStructUSRDATA>& _OUT subDataUSRDATA,

                                     bool isSort)
{
    subDataDESVAR.clear ();
    subDataDNODE.clear ();
    subDataDRESP1.clear ();
    subDataDRESP2.clear ();
    subDataDTABLE.clear ();
    subDataDVCREL1.clear ();
    subDataDVCREL2.clear ();
    subDataDVLREL1.clear ();
    subDataDVMREL1.clear ();
    subDataDVMREL2.clear ();
    subDataDVPREL1.clear ();
    subDataDVPREL2.clear ();
    subDataGRDTYP.clear ();
    subDataUSRDATA.clear ();

    IDENTITYData.clear ();
    IDENTITYData.reserve (own.size ());

    // 获取已有数据条数，主要是放在主表的POS字段
    // 如果是重新排序的时候，Pos都是从0 开始的
    size_t countDESVAR  = isSort ? 0 : this->GetDatasetInfo (SubDataset_DESVAR).GetRowCount ();
    size_t countDNODE   = isSort ? 0 : this->GetDatasetInfo (SubDataset_DNODE).GetRowCount ();
    size_t countDRESP1  = isSort ? 0 : this->GetDatasetInfo (SubDataset_DRESP1).GetRowCount ();
    size_t countDRESP2  = isSort ? 0 : this->GetDatasetInfo (SubDataset_DRESP2).GetRowCount ();
    size_t countDTABLE  = isSort ? 0 : this->GetDatasetInfo (SubDataset_DTABLE).GetRowCount ();
    size_t countDVCREL1 = isSort ? 0 : this->GetDatasetInfo (SubDataset_DVCREL1).GetRowCount ();
    size_t countDVCREL2 = isSort ? 0 : this->GetDatasetInfo (SubDataset_DVCREL2).GetRowCount ();
    size_t countDVLREL1 = isSort ? 0 : this->GetDatasetInfo (SubDataset_DVLREL1).GetRowCount ();
    size_t countDVMREL1 = isSort ? 0 : this->GetDatasetInfo (SubDataset_DVMREL1).GetRowCount ();
    size_t countDVMREL2 = isSort ? 0 : this->GetDatasetInfo (SubDataset_DVMREL2).GetRowCount ();
    size_t countDVPREL1 = isSort ? 0 : this->GetDatasetInfo (SubDataset_DVPREL1).GetRowCount ();
    size_t countDVPREL2 = isSort ? 0 : this->GetDatasetInfo (SubDataset_DVPREL2).GetRowCount ();
    size_t countGRDTYP  = isSort ? 0 : this->GetDatasetInfo (SubDataset_GRDTYP).GetRowCount ();
    size_t countUSRDATA = isSort ? 0 : this->GetDatasetInfo (SubDataset_USRDATA).GetRowCount ();

    // OwnIdent 代表咱们自己的数据结构
    // NasIdent 代表nastran的数据结构
    for (auto& OwnIdent : own)
    {
        // 写入附表
        subDataDESVAR.insert (subDataDESVAR.end (), OwnIdent.m_DESVARList.begin (), OwnIdent.m_DESVARList.end ());
        subDataDNODE.insert (subDataDNODE.end (), OwnIdent.m_DNODEList.begin (), OwnIdent.m_DNODEList.end ());
        subDataDRESP1.insert (subDataDRESP1.end (), OwnIdent.m_DRESP1List.begin (), OwnIdent.m_DRESP1List.end ());
        subDataDRESP2.insert (subDataDRESP2.end (), OwnIdent.m_DRESP2List.begin (), OwnIdent.m_DRESP2List.end ());
        subDataDTABLE.insert (subDataDTABLE.end (), OwnIdent.m_DTABLEList.begin (), OwnIdent.m_DTABLEList.end ());
        subDataDVCREL1.insert (subDataDVCREL1.end (), OwnIdent.m_DVCREL1List.begin (), OwnIdent.m_DVCREL1List.end ());
        subDataDVCREL2.insert (subDataDVCREL2.end (), OwnIdent.m_DVCREL2List.begin (), OwnIdent.m_DVCREL2List.end ());
        subDataDVLREL1.insert (subDataDVLREL1.end (), OwnIdent.m_DVLREL1List.begin (), OwnIdent.m_DVLREL1List.end ());
        subDataDVMREL1.insert (subDataDVMREL1.end (), OwnIdent.m_DVMREL1List.begin (), OwnIdent.m_DVMREL1List.end ());
        subDataDVMREL2.insert (subDataDVMREL2.end (), OwnIdent.m_DVMREL2List.begin (), OwnIdent.m_DVMREL2List.end ());
        subDataDVPREL1.insert (subDataDVPREL1.end (), OwnIdent.m_DVPREL1List.begin (), OwnIdent.m_DVPREL1List.end ());
        subDataDVPREL2.insert (subDataDVPREL2.end (), OwnIdent.m_DVPREL2List.begin (), OwnIdent.m_DVPREL2List.end ());
        subDataGRDTYP.insert (subDataGRDTYP.end (), OwnIdent.m_GRDTYPList.begin (), OwnIdent.m_GRDTYPList.end ());
        subDataUSRDATA.insert (subDataUSRDATA.end (), OwnIdent.m_USRDATAList.begin (), OwnIdent.m_USRDATAList.end ());

        const size_t lenDESVAR  = OwnIdent.m_DESVARList.size ();
        const size_t lenDNODE   = OwnIdent.m_DNODEList.size ();
        const size_t lenDRESP1  = OwnIdent.m_DRESP1List.size ();
        const size_t lenDRESP2  = OwnIdent.m_DRESP2List.size ();
        const size_t lenDTABLE  = OwnIdent.m_DTABLEList.size ();
        const size_t lenDVCREL1 = OwnIdent.m_DVCREL1List.size ();
        const size_t lenDVCREL2 = OwnIdent.m_DVCREL2List.size ();
        const size_t lenDVLREL1 = OwnIdent.m_DVLREL1List.size ();
        const size_t lenDVMREL1 = OwnIdent.m_DVMREL1List.size ();
        const size_t lenDVMREL2 = OwnIdent.m_DVMREL2List.size ();
        const size_t lenDVPREL1 = OwnIdent.m_DVPREL1List.size ();
        const size_t lenDVPREL2 = OwnIdent.m_DVPREL2List.size ();
        const size_t lenGRDTYP  = OwnIdent.m_GRDTYPList.size ();
        const size_t lenUSRDATA = OwnIdent.m_USRDATAList.size ();

        // 写入IDENTITY
        NasMainStruct NasIdent{};

        NasIdent.ID = OwnIdent.m_id;
        memcpy (NasIdent.LABEL, OwnIdent.m_LABEL, 8);
        memcpy (NasIdent.GRPNM, OwnIdent.m_GRPNM, 8);
        memcpy (NasIdent.TYPNM, OwnIdent.m_TYPNM, 8);
        NasIdent.REGION    = OwnIdent.m_REGION;
        NasIdent.NR3       = OwnIdent.m_NR3;
        NasIdent.DOMAIN_ID = OwnIdent.m_domainId;

        NasIdent.DESVAR_POS  = static_cast<I64> (countDESVAR);
        NasIdent.DNODE_POS   = static_cast<I64> (countDNODE);
        NasIdent.DRESP1_POS  = static_cast<I64> (countDRESP1);
        NasIdent.DRESP2_POS  = static_cast<I64> (countDRESP2);
        NasIdent.DTABLE_POS  = static_cast<I64> (countDTABLE);
        NasIdent.DVCREL1_POS = static_cast<I64> (countDVCREL1);
        NasIdent.DVCREL2_POS = static_cast<I64> (countDVCREL2);
        NasIdent.DVLREL1_POS = static_cast<I64> (countDVLREL1);
        NasIdent.DVMREL1_POS = static_cast<I64> (countDVMREL1);
        NasIdent.DVMREL2_POS = static_cast<I64> (countDVMREL2);
        NasIdent.DVPREL1_POS = static_cast<I64> (countDVPREL1);
        NasIdent.DVPREL2_POS = static_cast<I64> (countDVPREL2);
        NasIdent.GRDTYP_POS  = static_cast<I64> (countGRDTYP);
        NasIdent.USRDATA_POS = static_cast<I64> (countUSRDATA);

        NasIdent.DESVAR_LEN  = static_cast<I64> (lenDESVAR);
        NasIdent.DNODE_LEN   = static_cast<I64> (lenDNODE);
        NasIdent.DRESP1_LEN  = static_cast<I64> (lenDRESP1);
        NasIdent.DRESP2_LEN  = static_cast<I64> (lenDRESP2);
        NasIdent.DTABLE_LEN  = static_cast<I64> (lenDTABLE);
        NasIdent.DVCREL1_LEN = static_cast<I64> (lenDVCREL1);
        NasIdent.DVCREL2_LEN = static_cast<I64> (lenDVCREL2);
        NasIdent.DVLREL1_LEN = static_cast<I64> (lenDVLREL1);
        NasIdent.DVMREL1_LEN = static_cast<I64> (lenDVMREL1);
        NasIdent.DVMREL2_LEN = static_cast<I64> (lenDVMREL2);
        NasIdent.DVPREL1_LEN = static_cast<I64> (lenDVPREL1);
        NasIdent.DVPREL2_LEN = static_cast<I64> (lenDVPREL2);
        NasIdent.GRDTYP_LEN  = static_cast<I64> (lenGRDTYP);
        NasIdent.USRDATA_LEN = static_cast<I64> (lenUSRDATA);

        IDENTITYData.emplace_back (NasIdent);

        countDESVAR += lenDESVAR;
        countDNODE += lenDNODE;
        countDRESP1 += lenDRESP1;
        countDRESP2 += lenDRESP2;
        countDTABLE += lenDTABLE;
        countDVCREL1 += lenDVCREL1;
        countDVCREL2 += lenDVCREL2;
        countDVLREL1 += lenDVLREL1;
        countDVMREL1 += lenDVMREL1;
        countDVMREL2 += lenDVMREL2;
        countDVPREL1 += lenDVPREL1;
        countDVPREL2 += lenDVPREL2;
        countGRDTYP += lenGRDTYP;
        countUSRDATA += lenUSRDATA;
    }
}

void DRESP3Repository::NastranToOwn (std::vector<NasMainStruct>&    IDENTITYData,
                                     std::vector<SubStructDESVAR>&  subDataDESVAR,
                                     std::vector<SubStructDNODE>&   subDataDNODE,
                                     std::vector<SubStructDRESP1>&  subDataDRESP1,
                                     std::vector<SubStructDRESP2>&  subDataDRESP2,
                                     std::vector<SubStructDTABLE>&  subDataDTABLE,
                                     std::vector<SubStructDVCREL1>& subDataDVCREL1,
                                     std::vector<SubStructDVCREL2>& subDataDVCREL2,
                                     std::vector<SubStructDVLREL1>& subDataDVLREL1,
                                     std::vector<SubStructDVMREL1>& subDataDVMREL1,
                                     std::vector<SubStructDVMREL2>& subDataDVMREL2,
                                     std::vector<SubStructDVPREL1>& subDataDVPREL1,
                                     std::vector<SubStructDVPREL2>& subDataDVPREL2,
                                     std::vector<SubStructGRDTYP>&  subDataGRDTYP,
                                     std::vector<SubStructUSRDATA>& subDataUSRDATA,

                                     std::vector<MainStruct>& _OUT own)
{
    own.clear ();
    own.reserve (IDENTITYData.size ());

    // 代表初始的Index 位置,
    // 因为数据有可能是截取了附表的一部分，所有主表中根据POS找的时候需要对应一下，
    // 比如第一个主表数据中POS字段为10000， 附表的vector的数据
    // 就是从附表中的第10000个开始对应的 也就是Index==0的
    // const I64 index = IDENTITYData[0].POS;

    MainStruct const tempown;
    own.resize (IDENTITYData.size (), tempown);

    for (size_t i = 0; i < IDENTITYData.size (); i++)
    {
        auto& OwnIdent = own[i];
        auto& NasIdent = IDENTITYData[i];

        OwnIdent.m_id = NasIdent.ID;
        memcpy (OwnIdent.m_LABEL, NasIdent.LABEL, 8);
        memcpy (OwnIdent.m_GRPNM, NasIdent.GRPNM, 8);
        memcpy (OwnIdent.m_TYPNM, NasIdent.TYPNM, 8);
        OwnIdent.m_REGION   = NasIdent.REGION;
        OwnIdent.m_NR3      = NasIdent.NR3;
        OwnIdent.m_domainId = NasIdent.DOMAIN_ID;

        if (NasIdent.DESVAR_LEN != 0)
        {
            OwnIdent.m_DESVARList.assign (subDataDESVAR.begin () + NasIdent.DESVAR_POS - IDENTITYData[0].DESVAR_POS,
                                          subDataDESVAR.begin () + NasIdent.DESVAR_POS + NasIdent.DESVAR_LEN - IDENTITYData[0].DESVAR_POS);
        }
        if (NasIdent.DNODE_LEN != 0)
        {
            OwnIdent.m_DNODEList.assign (subDataDNODE.begin () + NasIdent.DNODE_POS - IDENTITYData[0].DNODE_POS,
                                         subDataDNODE.begin () + NasIdent.DNODE_POS + NasIdent.DNODE_LEN - IDENTITYData[0].DNODE_POS);
        }
        if (NasIdent.DRESP1_LEN != 0)
        {
            OwnIdent.m_DRESP1List.assign (subDataDRESP1.begin () + NasIdent.DRESP1_POS - IDENTITYData[0].DRESP1_POS,
                                          subDataDRESP1.begin () + NasIdent.DRESP1_POS + NasIdent.DRESP1_LEN - IDENTITYData[0].DRESP1_POS);
        }
        if (NasIdent.DRESP2_LEN != 0)
        {
            OwnIdent.m_DRESP2List.assign (subDataDRESP2.begin () + NasIdent.DRESP2_POS - IDENTITYData[0].DRESP2_POS,
                                          subDataDRESP2.begin () + NasIdent.DRESP2_POS + NasIdent.DRESP2_LEN - IDENTITYData[0].DRESP2_POS);
        }
        if (NasIdent.DTABLE_LEN != 0)
        {
            OwnIdent.m_DTABLEList.assign (subDataDTABLE.begin () + NasIdent.DTABLE_POS - IDENTITYData[0].DTABLE_POS,
                                          subDataDTABLE.begin () + NasIdent.DTABLE_POS + NasIdent.DTABLE_LEN - IDENTITYData[0].DTABLE_POS);
        }
        if (NasIdent.DVCREL1_LEN != 0)
        {
            OwnIdent.m_DVCREL1List.assign (subDataDVCREL1.begin () + NasIdent.DVCREL1_POS - IDENTITYData[0].DVCREL1_POS,
                                           subDataDVCREL1.begin () + NasIdent.DVCREL1_POS + NasIdent.DVCREL1_LEN
                                               - IDENTITYData[0].DVCREL1_POS);
        }
        if (NasIdent.DVCREL2_LEN != 0)
        {
            OwnIdent.m_DVCREL2List.assign (subDataDVCREL2.begin () + NasIdent.DVCREL2_POS - IDENTITYData[0].DVCREL2_POS,
                                           subDataDVCREL2.begin () + NasIdent.DVCREL2_POS + NasIdent.DVCREL2_LEN
                                               - IDENTITYData[0].DVCREL2_POS);
        }
        if (NasIdent.DVLREL1_LEN != 0)
        {
            OwnIdent.m_DVLREL1List.assign (subDataDVLREL1.begin () + NasIdent.DVLREL1_POS - IDENTITYData[0].DVLREL1_POS,
                                           subDataDVLREL1.begin () + NasIdent.DVLREL1_POS + NasIdent.DVLREL1_LEN
                                               - IDENTITYData[0].DVLREL1_POS);
        }
        if (NasIdent.DVMREL1_LEN != 0)
        {
            OwnIdent.m_DVMREL1List.assign (subDataDVMREL1.begin () + NasIdent.DVMREL1_POS - IDENTITYData[0].DVMREL1_POS,
                                           subDataDVMREL1.begin () + NasIdent.DVMREL1_POS + NasIdent.DVMREL1_LEN
                                               - IDENTITYData[0].DVMREL1_POS);
        }
        if (NasIdent.DVMREL2_LEN != 0)
        {
            OwnIdent.m_DVMREL2List.assign (subDataDVMREL2.begin () + NasIdent.DVMREL2_POS - IDENTITYData[0].DVMREL2_POS,
                                           subDataDVMREL2.begin () + NasIdent.DVMREL2_POS + NasIdent.DVMREL2_LEN
                                               - IDENTITYData[0].DVMREL2_POS);
        }
        if (NasIdent.DVPREL1_LEN != 0)
        {
            OwnIdent.m_DVPREL1List.assign (subDataDVPREL1.begin () + NasIdent.DVPREL1_POS - IDENTITYData[0].DVPREL1_POS,
                                           subDataDVPREL1.begin () + NasIdent.DVPREL1_POS + NasIdent.DVPREL1_LEN
                                               - IDENTITYData[0].DVPREL1_POS);
        }
        if (NasIdent.DVPREL2_LEN != 0)
        {
            OwnIdent.m_DVPREL2List.assign (subDataDVPREL2.begin () + NasIdent.DVPREL2_POS - IDENTITYData[0].DVPREL2_POS,
                                           subDataDVPREL2.begin () + NasIdent.DVPREL2_POS + NasIdent.DVPREL2_LEN
                                               - IDENTITYData[0].DVPREL2_POS);
        }
        if (NasIdent.GRDTYP_LEN != 0)
        {
            OwnIdent.m_GRDTYPList.assign (subDataGRDTYP.begin () + NasIdent.GRDTYP_POS - IDENTITYData[0].GRDTYP_POS,
                                          subDataGRDTYP.begin () + NasIdent.GRDTYP_POS + NasIdent.GRDTYP_LEN - IDENTITYData[0].GRDTYP_POS);
        }
        if (NasIdent.USRDATA_LEN != 0)
        {
            OwnIdent.m_USRDATAList.assign (subDataUSRDATA.begin () + NasIdent.USRDATA_POS - IDENTITYData[0].USRDATA_POS,
                                           subDataUSRDATA.begin () + NasIdent.USRDATA_POS + NasIdent.USRDATA_LEN
                                               - IDENTITYData[0].USRDATA_POS);
        }
    }
}
